#include "functions.h"

#include <math.h>

float pcg_abs(float x) {
	if (x < 0) return -x;
	return x;
}

float pcg_bias(float t, float b) {
	if (t < 0) return -pow(-t, log2f(b) / log2f(0.5));
	return pow(t, log2f(b) / log2f(0.5));
}

float pcg_gain(float t, float b) {
	char neg = 0;
	float v;
	if (t < 0) { neg = 1; t = -t; }
	if (t < 0.5) v = pcg_bias(1 - t, 2 * b);
	else v = pcg_bias(1 - t, 2 - 2 * b);
	
	if (neg) return -v;
	return v;
}

float pcg_lerp(float x1, float x2, float t) {
	return t * x2 + (t - 1) * x1;
}

float pcg_clamp(float v, float min, float max) {
	if (v < min) return min;
	if (v > max) return max;
	return v;
}